﻿using CCWin;
using ERP_SaaS_WinForm.登录.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;

using System.Threading.Tasks;




namespace ERP_SaaS_WinForm.登录
{
    public partial class ShowProducts : CCSkinMain
    {
        public ShowProducts()
        {
            InitializeComponent();
            this.Load += ShowProducts_Load;
        }
        public HttpClient client = new HttpClient();
        private async void ShowProducts_Load(object sender, EventArgs e)
        {
           var list= await ShowType();
            List<ProductTypes> data =  list;

            // 绑定到ComboBox
            // 注意：如果当前不在UI线程，需要切换到UI线程
            if (cbxtype.InvokeRequired)
            {
                cbxtype.Invoke(new Action(() =>
                {
                    cbxtype.DataSource = data;
                    cbxtype.ValueMember = "ProductType_Id";   // 设置显示成员
                    cbxtype.DisplayMember = "ProductTypeName";    // 设置值成员
                }));
            }
            else
            {
                cbxtype.DataSource = data;
                cbxtype.ValueMember = "ProductType_Id";
                cbxtype.DisplayMember = "ProductTypeName";
            }
        }


        /// 动态显示下拉框方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public async Task<List<ProductTypes>> ShowType()
        {
                var response = await client.GetAsync($"http://localhost:5279/api/SelectAll/ShowProductTypes");
            // 确保响应成功
            if (response.IsSuccessStatusCode)
            {
                response.EnsureSuccessStatusCode();//确保响应成功

                string json = await response.Content.ReadAsStringAsync();//字符串形式的json数据

                ApiResponse<ProductTypes> apiResponse = JsonConvert.DeserializeObject<ApiResponse<ProductTypes>>(json);//字符串json数据反序列化成ApiResponse对象

                // 步骤3: 提取data数据
                return apiResponse.data;
            }
            else
            {
                return new List<ProductTypes>();
            }
        }

      
        public async Task<ApiPaging<ApiResponse<ProductTypes>>> PageShow()
        {
            var response = await client.GetAsync($"http://localhost:5279/api/ProductIntory/GetProduct?PageIndex={1}&PageSize={2}");

            // 确保响应成功
            if (response.IsSuccessStatusCode)
            {
                response.EnsureSuccessStatusCode();//确保响应成功

                string json = await response.Content.ReadAsStringAsync();//字符串形式的json数据

                ApiPaging<ApiResponse<ProductTypes>> apiResponse = JsonConvert.DeserializeObject< ApiPaging<ApiResponse<ProductTypes>>>(json);//字符串json数据反序列化成ApiResponse对象

                // 步骤3: 提取data数据
                //return apiResponse.PData;
            }
            return null;
        }
           

        private void dtnget_Click(object sender, EventArgs e)
        {

        }


    }
}
